package com.fish.yssb.ui.page.util;

import android.content.Context;
import android.content.SharedPreferences;

/*
 * 使用方法
 * 1.先在application 初始化
 * 2.其他地方直接使用
 */
public class PrefeneceUtil {
    private static SharedPreferences sp;
    private static SharedPreferences.Editor editor;
    private static String privateName = "privateName";
    protected static String aeskey = "1234567890123456";


    private static PrefeneceUtil prefeneceUtil;

    public static PrefeneceUtil getInstance () {
        if ( prefeneceUtil == null ) {
            prefeneceUtil = new PrefeneceUtil();
        }
        return prefeneceUtil;
    }

    public  PrefeneceUtil()
    {

    }
//    public PrefeneceUtil(Context context, String name) {
//        sp = context.getSharedPreferences(name, Context.MODE_PRIVATE);
//        editor = sp.edit();
//    }

    public void initPrefenece(Context context) {
        sp = context.getSharedPreferences(privateName,
                Context.MODE_PRIVATE);
        editor = sp.edit();
    }


    public static void setData(String key, String sign, Boolean encry) {

        AesUtils.getInstance(aeskey);
        try {
            if (encry) {
                key = AesUtils.encrypt(key, aeskey);
                sign = AesUtils.encrypt(sign, aeskey);
            }
            editor.putString(key, sign);
            editor.commit();
        }catch (Exception ex)
        {
            ex.printStackTrace();
        }
    }

    public static String getData(String key, String defaultStr, Boolean encry) {
        AesUtils.getInstance(aeskey);
        String ret = "";
        try {
            if(encry)
            {
                key = AesUtils.encrypt(key,aeskey);
            }
            ret = sp.getString(key, defaultStr);
            if(encry)
            {
                ret = AesUtils.decrypt(ret);
            }
        }catch (Exception ex)
        {
            ex.printStackTrace();
        }
        return ret;
    }


}